package cn.itcast.orders.controller;


import cn.itcast.context.BaseContext;
import cn.itcast.orders.pojo.AddressBook;
import cn.itcast.result.Result;
import cn.itcast.orders.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;


    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return
     */
    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List> list(){
        log.info("查询当前登录用户的所有地址信息，当前用户表的id：{}", BaseContext.getCurrentId());
        List<AddressBook> addressBooks = addressBookService.list();

        return Result.success(addressBooks);
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result<AddressBook> addressBookResult(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.addressBookResult(addressBook);

        return Result.success();
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址")
    @GetMapping("{id}")
    public Result<AddressBook> getByUserId(@PathVariable Integer id){
        log.info("根据id查询地址：{}",id);
        AddressBook addressBook = addressBookService.getById(id);

        return Result.success(addressBook);
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("根据id修改地址")
    @PutMapping()
    public Result<String> updateById(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址:{}",addressBook);
        addressBookService.updateById(addressBook);

        return Result.success();
    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result<String> deleteById(Integer id){
        log.info("根据id删除地址:{}",id);
        addressBookService.deleteById(id);

        return Result.success();
    }

    /**
     * 根据地址表id设置默认地址
     *
     * @param map
     * @return
     */
    @ApiOperation("根据地址表id设置默认地址")
    @PutMapping("/default")
    public Result<String> updateDefaultAddress(@RequestBody Map<String, Long> map){
        log.info("根据地址表id设置默认地址:{}",map.get("id"));
        addressBookService.updateDefaultAddress(map.get("id"));

        return Result.success();
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        log.info("查询默认地址,当前用户：{}",BaseContext.getCurrentId());
        AddressBook addressBook = addressBookService.getDefaultAddressByUserId();

        return Result.success(addressBook);
    }

}
